package thread;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class demo28 {
    public static void main(String[] args) {
        //通过callable 来描述一个这样的任务~
        Callable<Integer> callable = new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                int sum = 0;
                for (int i = 1; i <= 1000; i++) {
                    sum+=i;
                }
                return sum;
            }
        };
        //为了让线程执行callable任务 还需要一个辅助类FutureTask 来
        FutureTask<Integer> futureTask = new FutureTask<>(callable);
        //创建线程来完成这里的计算工作。
        Thread t = new Thread(futureTask);
        t.start();
        //凭小票来端麻辣烫
        //如果线程的任务还没执行完 get就会阻塞 直到任务完成
        try {
            System.out.println(futureTask.get());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }
}
